package com.op.teachingliteapp.fragment.viewer;


import android.app.Activity;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Color;
import android.graphics.RectF;
import android.os.AsyncTask;
import android.util.DisplayMetrics;
import android.util.Log;
import android.util.LruCache;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;

import com.op.teachingliteapp.R;
import com.op.teachingliteapp.sender.ui.view.PdfViewPager;
import com.op.teachingliteapp.util.ScreenSendUtils;
import com.polites.android.GestureImageView;

import org.vudroid.core.codec.CodecDocument;
import org.vudroid.core.codec.CodecPage;
import org.vudroid.pdfdroid.codec.PdfContext;

/**
 * Created by Genji on 2015/7/5.
 */
public class PDFAdapter extends BasicViewPagerAdapter {
    private String TAG = this.getClass().getSimpleName();
    private CodecDocument pdfDocument;
    private int pageCount;
    private PdfContext pdfContext;
    private String uri;
    //    private PDFBitmapCache pdfBitmapCache;
    private RectF rf;
    private Activity activity;
    private int screenWidth;
    private int screenHeight;
    private Integer index = 0;

    public PdfViewPager getPdfViewPager() {
        return pdfViewPager;
    }

    public void setPdfViewPager(PdfViewPager pdfViewPager) {
        this.pdfViewPager = pdfViewPager;
    }

    private PdfViewPager pdfViewPager;


    public PDFAdapter(Activity activity, String uri) {


        this.activity = activity;

        DisplayMetrics dm = new DisplayMetrics();
        WindowManager windowMgr = (WindowManager) this.activity.getSystemService(Context.WINDOW_SERVICE);
        windowMgr.getDefaultDisplay().getMetrics(dm);
        this.screenHeight = dm.heightPixels;
        this.screenWidth = dm.widthPixels;
        this.uri = uri;
        this.pdfContext = new PdfContext();
        this.pdfDocument = this.pdfContext.openDocument(uri);
        this.pageCount = pdfDocument.getPageCount();
//////        int maxMemory = (int) (Runtime.getRuntime().maxMemory() / 1024);
////        int cacheSize = maxMemory / 8;
//        this.pdfBitmapCache = new PDFBitmapCache(cacheSize);
        this.rf = new RectF();
        this.rf.bottom = this.rf.right = (float) 1.0;


    }

    @Override
    public void destroyItem(ViewGroup container, int position, Object object) {
        container.removeView((View) object);
    }


    @Override
    public View instantiateItem(ViewGroup container, int position) {
        Log.i(TAG, "instantiateItem : " + position);
        GestureImageView gestureImageView = new GestureImageView(activity);

        gestureImageView.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT));
        gestureImageView.setBackgroundColor(Color.DKGRAY);
        gestureImageView.setImageResource(R.drawable.thumbnail_bg_doc);
        gestureImageView.setId(position);

        container.addView(gestureImageView, 0);
        new PdfBitmapLoader(gestureImageView).execute(position);
        return gestureImageView;
    }


    @Override
    public int getCount() {
        return this.pdfDocument.getPageCount();
    }

    @Override
    public boolean isViewFromObject(View view, Object o) {
        return view == o;
    }




    //--------------------------------------------------------
    private class PdfBitmapLoader extends AsyncTask<Integer, Bitmap, Void> {
        private GestureImageView gestureImageView;
        private Integer loadIndex = 0;



        @Override
        protected void onProgressUpdate(Bitmap... values) {
            super.onProgressUpdate(values);
            this.gestureImageView.setImageBitmap(values[0]);
//            Log.i(TAG, "LoadCompleted : "+loadIndex+" sending ");
            if (pdfViewPager.getCurrentItem()==this.gestureImageView.getId()) {
                ScreenSendUtils.sendWithDely(this.gestureImageView);
            }



            Log.i(TAG, "view id : " + gestureImageView.getId());
//
        }

        public PdfBitmapLoader(GestureImageView gestureImageView) {
            this.gestureImageView = gestureImageView;

        }

        @Override
        protected Void doInBackground(Integer... params) {
            loadIndex = params[0];
            Log.i(TAG, "doInBackground start " + "loadIndex = " + loadIndex + " index = " + index);
            Bitmap bitmap;
//            bitmap = pdfBitmapCache.getBitmapFromMemCache(params[0]);
//            if (bitmap == null) {
//                Log.i(TAG, "doInBackground " + params[0] + " page is not in cache");
            if (screenWidth*screenHeight>1280*800) {
                screenHeight=screenHeight/2;
                screenWidth =screenWidth/2;
            }
            CodecPage pdfPage = pdfDocument.getPage(params[0]);

            bitmap = pdfPage.renderBitmap(screenWidth, screenHeight, rf);
//                pdfBitmapCache.addBitmapToMemoryCache(params[0], bitmap);
//            }
            publishProgress(bitmap);
            Log.i(TAG, "doInBackground end");
            return null;
        }
    }

    private class PDFBitmapCache extends LruCache<Integer, Bitmap> {

        public PDFBitmapCache(int maxSize) {
            super(maxSize);
        }


        @Override
        protected int sizeOf(Integer key, Bitmap value) {
            return value.getByteCount() / 1024;
        }

        public void addBitmapToMemoryCache(Integer key, Bitmap value) {
            if (getBitmapFromMemCache(key) == null) {
                this.put(key, value);
            }
        }

        public Bitmap getBitmapFromMemCache(Integer key) {
            return this.get(key);
        }

    }

}
